import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_devices/bruno/src/components/button/brn_normal_button.dart';
import 'package:smart_devices/routes/index.dart';
import 'package:smart_devices/ui/draw_under_line.dart';
import 'package:smart_devices/ui/max_line_wrap.dart';
import 'package:smart_devices/ui/scroll_refresh_config.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';
import 'package:smart_devices/utils/tools.dart';

import 'order_logic.dart';

class RepairOrderView extends GetView<RepairOrderLogic> {
  const RepairOrderView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back_ios,
            color: Colors.white,
            size: 15,
          ),
          onPressed: Get.back,
        ),
        automaticallyImplyLeading: false,
        title: Text(
          '维修工单',
          style: StylesUtils.customTextStyle(
              fontSize: 18, fontWeight: FontWeight.w700),
        ),
        centerTitle: true,
        bottom: PreferredSize(
            preferredSize: const Size.fromHeight(50),
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 7),
              decoration: const BoxDecoration(color: Colors.white),
              child: Row(
                children: [
                  Expanded(
                      child: Container(
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(5),
                        border: Border.all(
                            color: ColorsUtils.parseColorFromHexString(
                                ColorsUtils.colorE4e4))),
                    child: Row(
                      children: [
                        Container(
                          width: 36,
                          alignment: Alignment.center,
                          child: ToolsUtils.urlSvg(
                              'https://cdn7.axureshop.com/demo2023/2163241/images/常用组件/u2227.svg'),
                        ),
                        Expanded(
                            child: Obx(() => TextField(
                                  textDirection: TextDirection.ltr,
                                  controller: TextEditingController(
                                      text: controller.search.value),
                                  cursorColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  style: StylesUtils.customTextStyle(
                                      fontSize: 16,
                                      color: ColorsUtils.color1c24),
                                  textAlign: TextAlign.left,
                                  textAlignVertical: TextAlignVertical.center,
                                  cursorWidth: 2,
                                  // cursorHeight: 18,
                                  cursorRadius: const Radius.circular(2),
                                  decoration: InputDecoration(
                                      hintText: '请输入工单编码/工单主题/备件名称',
                                      hintStyle: StylesUtils.customTextStyle(
                                          color: ColorsUtils.colorCccc,
                                          fontSize: 13),
                                      isDense: true,
                                      isCollapsed: true,
                                      border: InputBorder.none,
                                      focusColor:
                                          ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.color1c24),
                                      contentPadding:
                                          const EdgeInsets.symmetric(
                                              vertical: 13),
                                      focusedBorder: InputBorder.none),
                                  onChanged: (String text) {
                                    controller.search.value = text;
                                  },
                                )))
                      ],
                    ),
                  )),
                  InkWell(
                    onTap: () {
                      // Get.toNamed(Routes.deviceSpareScreen);
                    },
                    child: Container(
                      width: 60,
                      alignment: Alignment.centerRight,
                      child: Text.rich(TextSpan(children: [
                        WidgetSpan(
                            alignment: PlaceholderAlignment.middle,
                            child: ToolsUtils.urlSvg(
                                'https://cdn7.axureshop.com/demo2023/2163241/images/常用组件/u2222.svg',
                                width: 15,
                                height: 15)),
                        TextSpan(
                            text: '刷选',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color3333))
                      ])),
                    ),
                  )
                ],
              ),
            )),
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  Widget scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  Widget listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (BuildContext c, int index) {
        return DrawUnderLine(
            child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 15),
          margin: index == 0 ? const EdgeInsets.only(top: 15) : EdgeInsets.zero,
          decoration: const BoxDecoration(color: Colors.white),
          child: Column(
            children: [
              Container(
                height: 40,
                alignment: Alignment.centerLeft,
                child: Text(
                  controller.pageList[index].title,
                  style: StylesUtils.customTextStyle(
                      color: ColorsUtils.color0052,
                      fontSize: 16,
                      fontWeight: FontWeight.w700),
                ),
              ),
              Container(
                height: 30,
                alignment: Alignment.centerLeft,
                child: MaxLineWrap(
                  maxLine: 1,
                  itemAlignment: MaxLineWrapAlignment.start,
                  children: [
                    Text(
                      '维修单号',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333, fontSize: 13),
                    ),
                    Text(
                      controller.pageList[index].orderNo,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.colorAaaa, fontSize: 13),
                    )
                  ],
                ),
              ),
              Container(
                height: 30,
                alignment: Alignment.centerLeft,
                child: MaxLineWrap(
                  maxLine: 1,
                  itemAlignment: MaxLineWrapAlignment.start,
                  children: [
                    Text(
                      '设备名称',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333, fontSize: 13),
                    ),
                    Text(
                      controller.pageList[index].name,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.colorAaaa, fontSize: 13),
                    )
                  ],
                ),
              ),
              Container(
                height: 30,
                alignment: Alignment.centerLeft,
                child: MaxLineWrap(
                  maxLine: 1,
                  itemAlignment: MaxLineWrapAlignment.start,
                  children: [
                    Text(
                      '设备编号',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333, fontSize: 13),
                    ),
                    Text(
                      controller.pageList[index].code,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.colorAaaa, fontSize: 13),
                    )
                  ],
                ),
              ),
              Container(
                height: 30,
                alignment: Alignment.centerLeft,
                child: MaxLineWrap(
                  maxLine: 1,
                  itemAlignment: MaxLineWrapAlignment.start,
                  children: [
                    Text(
                      '存放位置',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333, fontSize: 13),
                    ),
                    Text(
                      controller.pageList[index].orderNo,
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.colorAaaa, fontSize: 13),
                    )
                  ],
                ),
              ),
              Container(
                height: 30,
                alignment: Alignment.centerLeft,
                child: MaxLineWrap(
                  maxLine: 1,
                  itemAlignment: MaxLineWrapAlignment.start,
                  children: [
                    Text(
                      '紧急程度',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333, fontSize: 13),
                    ),
                    Text(
                      controller.pageList[index].degree.label,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.colorAaaa, fontSize: 13),
                    )
                  ],
                ),
              ),
              SizedBox(
                height: 40,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      controller.pageList[index].addTime,
                      style: StylesUtils.customTextStyle(
                          fontSize: 13, color: ColorsUtils.color3333),
                    ),
                    BrnNormalButton(
                      onTap: () {
                        Get.toNamed(Routes.deviceRepairInfo, arguments: {
                          'order': controller.pageList[index],
                          'details': controller.details.value,
                        });
                      },
                      insertPadding: const EdgeInsets.only(top: 2),
                      constraints:
                          const BoxConstraints.expand(width: 60, height: 25),
                      text: controller.pageList[index].status.label,
                      textStyle: StylesUtils.customTextStyle(
                          fontSize: 12, color: ColorsUtils.color0052),
                      decoration: BoxDecoration(
                          border: Border.all(
                        color: ColorsUtils.parseColorFromHexString(
                            ColorsUtils.color0052),
                      )),
                    )
                  ],
                ),
              )
            ],
          ),
        ));
      },
      itemCount: controller.pageList.length,
      itemExtent: 250,
    );
  }
}
